﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using System.Collections.Generic;
using System.Text;
using PowerCreator.LMS.Web.User;
using PowerCreator.LMS.Web.Project;
using PowerCreator.LMS.User.Entity;
using PowerCreator.LMS.Common.Script;
using PowerCreator.LMS.Project.Entity;
using PowerCreator.LMS.Web.Task;
using PowerCreator.LMS.Task.Entity;

public partial class sysadmin_TaskManager_TaskClassUserChecked : PowerCreator.LMS.Web.Base.BasePage
{
    ProjectUser opProjectUser = new ProjectUser();
    Department opDepartment = new Department();
    User opUser = new User();
    Project opProject = new Project();
    TaskClassManag objTaskClassManag = new TaskClassManag();
    TaskManag objTaskManag = new TaskManag();
    TaskClassUserManag objTaskClassUserManag = new TaskClassUserManag();
    TaskClassDepartmentManag objTaskClassDepartmentManag = new TaskClassDepartmentManag();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BinderSex();
            InitPage();

            string taskClassID = Request.QueryString["TaskClassID"].ToString();
            //string projectclassid = Request.QueryString["ProjectClassID"].ToString();
            ViewState["TaskClassID"] = taskClassID;

            BinderPage();

            BinderDepartment();
            BinderDepartmentClass();

            string search = GetSearchDepartment();
            BinderPagerDepartment(search);
            BinderRPTDepartment(search, 1);

            search = GetSearchUser();
            BinderPagerUser(search);
            BinderRPTUser(search, 1);
        }
    }
    protected override void InitPage()
    {
        chkAll.Text = GetLanageValueByKey("RoleUser_rdbtn_YesNoAll", PowerCreator.LMS.Language.LanagetItemType.Page);
        chkCancel.Text = GetLanageValueByKey("SetBaseAction_rdbtn_All", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnBack.Text = GetLanageValueByKey("btn_Reset", PowerCreator.LMS.Language.LanagetItemType.Page);
        
        btn_Search.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btn_SortSearch.Text = GetLanageValueByKey("btn_Search", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnDel.Text = GetLanageValueByKey("ManagerRole_gv_lnk_o2", PowerCreator.LMS.Language.LanagetItemType.Page);
        btnDel.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");

        base.InitPage();
    }
    protected void rpt_Department_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_del");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        }
    }
    protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
        {
            LinkButton lbtn = (LinkButton)e.Item.FindControl("lnkbtn_addclass");
            lbtn.Attributes.Add("onClick", "javascript:return confirm('确定删除此记录？');");
        }
    }


    private void BinderPage()
    {
        ViewState["ProjectClassUserType"] = "1";    //班级用户类型,1学生用户，2教师用户
        TaskClass objTaskClass = objTaskClassManag.Load(int.Parse(ViewState["TaskClassID"].ToString()));
        ViewState["ProjectClassUserType"] = objTaskClass.TaskCsUserType;
        ViewState["TaskID"] = objTaskClass.FKTaskID;
        lbl_ClassName.Text = objTaskClass.TaskCsName;
        lbl_ClassUserCount.Text = objTaskClass.TaskCsUserCount.ToString();

        VOTask objVOTask = objTaskManag.Load(objTaskClass.FKTaskID);
        lbl_ProjectName.Text = objVOTask.TaskName;


        //VOTTrainClass voProjectClass = opProjectUser.LoadProjectClass(int.Parse(ViewState["TaskClassID"].ToString()));
        //ViewState["ProjectClassUserType"] = voProjectClass.TrainclUserType.ToString();
        //ViewState["ProjectID"] = voProjectClass.FKTrainclaTrainproID.ToString();
        //lbl_ClassName.Text = voProjectClass.TrainclaName;
        //lbl_ClassUserCount.Text = voProjectClass.TrainclaStudentNum.ToString();
        //VOTTrainProject voProject = opProject.LoadTrainProject(voProjectClass.FKTrainclaTrainproID);
        //lbl_ProjectName.Text = voProject.TrainproName;
    }

    #region 部门信息

    private string GetSearchDepartment()
    {
        string search = string.Format("FKTaskCsID = {0}", ViewState["TaskClassID"]);
        return search;
    }
    private void BinderPagerDepartment(string search)
    {
        AspNetPager1.PageSize = LMSPageSize;
        AspNetPager1.RecordCount = objTaskClassDepartmentManag.Count(search);
    }
    private void BinderRPTDepartment(string search, int pageIndex)
    {
        string search1 = " DepartmentId  in( select FKTaskCDteptID from TaskClassDepartment where FKTaskCsID=" + ViewState["TaskClassID"].ToString() + ")";
        DataTable dt = opDepartment.Query(search1, "", LMSPageSize, pageIndex);

        rpt_Department.DataSource = dt;
        rpt_Department.DataBind();
        PreAspNetPager(AspNetPager1);
    }



    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearchDepartment();
        BinderRPTDepartment(search, AspNetPager1.CurrentPageIndex);
    }
    protected void rpt_Department_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "ldel")
        {
            string searchTaskCdt = string.Format(" FKTaskCsID={0} and FKTaskCDteptID={1}", ViewState["TaskClassID"].ToString(), e.CommandArgument.ToString());
            DataTable dtTaskCdt = objTaskClassDepartmentManag.Query(searchTaskCdt);
            int taskCdtID=int.Parse(dtTaskCdt.Rows[0]["TaskCdtID"].ToString());


            if (objTaskClassDepartmentManag.Remove(taskCdtID))
            {
            

                string search = GetSearchDepartment();
                BinderPagerDepartment(search);
                BinderRPTDepartment(search, AspNetPager1.CurrentPageIndex);

                search = GetSearchUser();
                BinderPagerUser(search);
                BinderRPTUser(search, AspNetPager2.CurrentPageIndex);

               
            }
        }
        else if (e.CommandName.ToLower() == "lshowuser")
        {
            //查看部门人员
        }
    }
    #endregion


    protected void rpt_User_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName.ToLower() == "ldelclassuser")
        {
            string search1 = string.Format("FKTrainclastuUserID = {0} and FKTrainclastuTrainclaID =  {1}", e.CommandArgument.ToString(), ViewState["TaskClassID"]);
            DataTable dt = opProjectUser.QueryProjectClassUser(search1, 1, 1);
            if (dt.Rows.Count != 0)
            {
                if (opProjectUser.DelProjectClassUser(int.Parse(dt.Rows[0]["TrainclastuID"].ToString())))
                {
                    string search = GetSearchUser();
                    BinderPagerUser(search);
                    BinderRPTUser(search, AspNetPager2.CurrentPageIndex);
                }
            }
        }
    }

    protected void AspNetPager2_PageChanged(object sender, EventArgs e)
    {
        string search = GetSearchUser();
        BinderRPTUser(search, AspNetPager2.CurrentPageIndex);
    }

    #region 绑定个人用户信息


    private string GetSearchUser()
    {
        StringBuilder sbSearch = new StringBuilder();
        if (!string.IsNullOrEmpty(txt_UserName.Text))
        {
            sbSearch.Append(string.Format(" UserName like '%{0}%' and ", txt_UserName.Text));
        }

        if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Student).ToString())
        {
            if (!string.IsNullOrEmpty(txt_RealName.Text))
            {
                sbSearch.Append(string.Format(" StudentName like '%{0}%' and ", txt_RealName.Text));
            }
            if (ddl_DepartmentClass.Items.Count != 0)
            {
                string classid = ddl_DepartmentClass.SelectedValue.ToString();
                if (classid != "0")
                    sbSearch.Append(string.Format(" DepartmentId = {0} and ", classid));
            }
            if (ddl_Sex.SelectedValue.ToString() != "2")
            {
                sbSearch.Append(string.Format(" StudentSex = {0} and ", ddl_Sex.SelectedValue.ToString()));
            }
        }
        else if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Teahcer).ToString())
        {
            if (!string.IsNullOrEmpty(txt_RealName.Text))
            {
                sbSearch.Append(string.Format(" TeadutyName like '%{0}%' and ", txt_RealName.Text));
            }
            if (ddl_Department.Items.Count != 0)
            {
                string departmentid = ddl_Department.SelectedValue.ToString();
                if (departmentid != "0")
                {
                    sbSearch.Append(string.Format(" DepartmentId = {0} and ", departmentid));
                }
            }
            if (ddl_Sex.SelectedValue.ToString() != "2")
            {
                sbSearch.Append(string.Format(" TeacherSex = {0} and ", ddl_Sex.SelectedValue.ToString()));
            }
        }

        if (ddl_UserIscheck.SelectedValue.ToString() != "3")
        {
            sbSearch.Append(string.Format("UserIscheck = {0} and ", ddl_UserIscheck.SelectedValue.ToString()));
        }

        sbSearch.Append(string.Format(" UserID in (select UserID from VIEW_TaskUser WHERE TaskCsID={0}) ", ViewState["TaskClassID"]));

        if (sbSearch.ToString().EndsWith("and "))
            sbSearch.Remove(sbSearch.ToString().LastIndexOf("and "), 4);

        return sbSearch.ToString();
    }

    //填充数据
    private List<UserInfo> FileList(DataTable dt)
    {
        List<UserInfo> list = new List<UserInfo>();
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            UserInfo item = new UserInfo();

            item.UserID = int.Parse(dt.Rows[i]["UserId"].ToString());
            item.UserName = dt.Rows[i]["UserName"].ToString();
            item.UserIscheck = int.Parse(dt.Rows[i]["UserIscheck"].ToString());
            item.UserType = int.Parse(dt.Rows[i]["UserType"].ToString());

            if (item.UserType == 0)
            {
                item.DepartmentName = "-";
                item.DepartmentClassName = dt.Rows[i]["DepartmentName"].ToString();
                item.RealName = dt.Rows[i]["StudentName"].ToString();
                item.UserSex = opUser.GetUserSexByData(int.Parse(dt.Rows[i]["StudentSex"].ToString()), GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page), GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page));
                item.DepartmentID = int.Parse(dt.Rows[i]["FKStudentDepartmentId"].ToString());
            }
            else if (item.UserType == 1)
            {
                item.DepartmentName = dt.Rows[i]["DepartmentName"].ToString();
                item.DepartmentClassName = "-";
                item.RealName = dt.Rows[i]["TeacherName"].ToString();
                item.UserSex = opUser.GetUserSexByData(int.Parse(dt.Rows[i]["TeacherSex"].ToString()), GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page), GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page));
                item.DepartmentID = int.Parse(dt.Rows[i]["FKTeacherDepartmentId"].ToString());
            }
            list.Add(item);
        }
        return list;
    }
    protected void BinderSex()
    {
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "2";

        ListItem li0 = new ListItem();
        li0.Text = GetLanageValueByKey("EditUser_desc27", PowerCreator.LMS.Language.LanagetItemType.Page);
        li0.Value = "0";
        ListItem li1 = new ListItem();
        li1.Text = GetLanageValueByKey("EditUser_desc28", PowerCreator.LMS.Language.LanagetItemType.Page);
        li1.Value = "1";
        ddl_Sex.Items.Add(li);
        ddl_Sex.Items.Add(li0);
        ddl_Sex.Items.Add(li1);
    }
    private void BinderPagerUser(string search)
    {
        AspNetPager2.PageSize = LMSPageSize;
        if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Student).ToString())
        {
            AspNetPager2.RecordCount = opUser.QueryVMAccountInfomationStudentCount(search);
        }
        else if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Teahcer).ToString())
        {
            AspNetPager2.RecordCount = opUser.QueryVMAccountInfomationTeacherCount(search);
        }
    }

    private void BinderRPTUser(string search, int pageIndex)
    {
        DataTable dt = new DataTable();
        List<UserInfo> list = new List<UserInfo>();

        if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Student).ToString())
        {
            dt = opUser.QueryVMAccountInfomaitionStudent(search, LMSPageSize, pageIndex);
        }
        else if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Teahcer).ToString())
        {
            dt = opUser.QueryVMAccountInfomaitionTeacher(search, LMSPageSize, pageIndex);
        }
        list = FileList(dt);
        rpt_User.DataSource = list;
        rpt_User.DataBind();
        PreAspNetPager(AspNetPager2);
    }

    #endregion
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        string search = GetSearchUser();
        BinderPagerUser(search);
        BinderRPTUser(search, 1);
    }
    protected void ddl_Department_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ViewState["ProjectClassUserType"].ToString() == Convert.ToInt16(ProjectClassTrainclUserType.Student).ToString())
        {
            BinderDepartmentClass();
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "showmy1", "<script>showFloat()</script>");
        }
    }
    private void BinderDepartment()
    {
        string search = "DepartmentType=1";
        DataTable dt = opDepartment.Query(search);
        ArrayList list = opDepartment.GetDepartmentTree(dt);
        ddl_Department.DataSource = list;
        ddl_Department.DataTextField = "Text";
        ddl_Department.DataValueField = "Value";
        ddl_Department.DataBind();
        ListItem li = new ListItem();
        li.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
        li.Value = "0";
        ddl_Department.Items.Insert(0, li);
    }
    private void BinderDepartmentClass()
    {
        if (!string.IsNullOrEmpty(ddl_Department.SelectedValue))
        {
            VOTDepartment vo = opDepartment.Load(int.Parse(ddl_Department.SelectedValue.ToString()));
            if (vo != null)
            {
                string search = "DepartmentType=2 and DepartmentFatherId=" + vo.DepartmentId.ToString();
                DataTable dt = opDepartment.Query(search);
                ddl_DepartmentClass.DataSource = dt;
                ddl_DepartmentClass.DataTextField = "DepartmentName";
                ddl_DepartmentClass.DataValueField = "DepartmentId";
                ddl_DepartmentClass.DataBind();
            }
            ListItem li = new ListItem();
            li.Text = GetLanageValueByKey("EditUser_desc29", PowerCreator.LMS.Language.LanagetItemType.Page);
            li.Value = "0";
            ddl_DepartmentClass.Items.Insert(0, li);
        }
    }
    protected void chkAll_CheckedChanged(object sender, EventArgs e)
    {
        chkCancel.Checked = false;
        //全选 / 取消
        for (int i = 0; i < rpt_User.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_User.Items[i].FindControl("chkSelect");
            if (chkAll.Checked)
                cbox.Checked = true;
            else
                cbox.Checked = false;
        }
    }
    protected void chkCancel_CheckedChanged(object sender, EventArgs e)
    {
        chkAll.Checked = false;
        //反选
        for (int i = 0; i < rpt_User.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_User.Items[i].FindControl("chkSelect");
            if (cbox.Checked)
                cbox.Checked = false;
            else
                cbox.Checked = true;
        }
    }
    protected void btnDel_Click(object sender, EventArgs e)
    {
        bool blupdate = false;
        for (int i = 0; i < rpt_User.Items.Count; i++)
        {
            CheckBox cbox = (CheckBox)rpt_User.Items[i].FindControl("chkSelect");
            Label lblid = (Label)rpt_User.Items[i].FindControl("lblid");

            if (cbox.Checked)
            {
                string search1 = string.Format("FKTaskCurUserID = {0} and FKTaskCSID =  {1}", lblid.Text, ViewState["TaskClassID"]);
                DataTable dt = objTaskClassUserManag.Query(search1);
                if (dt.Rows.Count != 0)
                {
                    if (objTaskClassUserManag.Remove(int.Parse(dt.Rows[0]["TaskCurID"].ToString())))
                    {
                        blupdate = true;
                    }
                }
            }
        }
        if (blupdate)
        {
            string search = GetSearchUser();
            BinderPagerUser(search);
            BinderRPTUser(search, AspNetPager2.CurrentPageIndex);
        }
    }
    protected void btn_SortSearch_Click(object sender, EventArgs e)
    {

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "TaskClassManager.aspx?id=" + ViewState["TaskID"].ToString();
        Script.RedirectURL(url);
    }
}
